using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace MWang.Generator
{
    public partial class ReverseItem : Form
    {
        public ReverseItem()
        {
            InitializeComponent();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                textBox1.Text = ofd.FileName;
                textBox2.Text = Path.Combine(Path.GetDirectoryName(ofd.FileName), "dest.txt");
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            FileStream ofs = new FileStream(textBox1.Text, FileMode.Open, FileAccess.Read);
            StreamReader ordr = new StreamReader(ofs,Encoding.Default);
            FileStream dfs = new FileStream(textBox2.Text, FileMode.Create, FileAccess.Write);
            StreamWriter dwtr = new StreamWriter(dfs,Encoding.Default);
            while (!ordr.EndOfStream)
            {
                string curLine = ordr.ReadLine();
                string[] var = curLine.Split('=');
                if (var.Length > 1)
                {
                    string newLine = var[1].Replace(";", string.Empty).Trim();
                    newLine += "=" + var[0].Trim() + ";";
                    dwtr.WriteLine(newLine);
                }
                else
                {
                    dwtr.WriteLine(curLine);
                }
            }
            dwtr.Close();
            dfs.Close();
            ordr.Close();
            ofs.Close();
            System.Diagnostics.Process.Start("notepad.exe \"" + textBox2.Text+"\"");
        }
    }
}